<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;

/**
 * 订单售后操作日志
 */
class orderaftersalelogModel extends Model
{

    // 表名,不含前缀
    protected $table = 'order_aftersale_log';

    // //列表动态隐藏字段
    // protected static $list_hidden = ['content', 'params', 'images', 'service_ids'];

    protected $appends = [];

    protected function serializeDate($date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function operAdd($order = null, $aftersale = null, $oper = null, $type = 'user', $data = [])
    {
        $oper_id = empty($oper) ? 0 : (is_array($oper) ? $oper['id'] : $oper->id);
        $images = $data['images'] ?? [];
        $images = is_array($images) ? implode(',', $images) : $images;
        
        $self = new self();
        $self->order_id = $order['id'] ?? ($aftersale['order_id'] ?? 0);
        $self->order_aftersale_id = is_null($aftersale) ? 0 : $aftersale['id'];
        $self->oper_type = $type;
        $self->oper_id = $oper_id;
        $self->dispatch_status = is_null($aftersale) ? 0 : $aftersale['dispatch_status'];
        $self->aftersale_status = is_null($aftersale) ? 0 : $aftersale['aftersale_status'];
        $self->refund_status = is_null($aftersale) ? 0 : $aftersale['refund_status'];
        $self->reason = $data['reason'] ?? '';
        $self->content = $data['content'] ?? '';
        $self->images = $images;
        $self->save();

        return $self;
    }


    public function getImagesAttribute()
    {
        return explode(',', $this->attributes['images']);
    }
}
